//
// Created by Administrator on 2021/6/5.
//
#include <jni.h>
#include <string>

/**
 * 还原出color值
 */
int getRGBOne(float i, bool long_) {
    if (long_) return (int) (i * 255 / 63);
    else return (int) (i * 255 / 31);
}

int getRGB(short sh) {
    int r = (sh & 0xF8) >> 3;
    int b = (sh & 0x1F00) >> 8;
    int g1 = (sh & 0x7) << 3;
    int g2 = (sh & 0xE000) >> 13;
    int red = getRGBOne(r, false);
    int green = getRGBOne(g1 + g2, true);
    int blue = getRGBOne(b, false);
    return 0xff000000 | (red << 16) | (green << 8) | blue;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xcc_mrpbmp_BmpLoad_pullColor(JNIEnv *env, jclass clazz, jintArray colors, jint size,
                                      jbyteArray bytes) {
    int *colorArray = env->GetIntArrayElements(colors, NULL);
    jbyte *byteArray = env->GetByteArrayElements(bytes, NULL);

    for (int i = 0; i < size; i++) {
        int i1 = i << 1;
        jbyte b1 = byteArray[i1];
        jbyte b2 = byteArray[i1 + 1];
        int it = b1;
        it = (it << 8) + b2;
        colorArray[i] = getRGB((short) it);
    }
}